package com.jbl.te.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jbl.commonutils.R;
import com.jbl.te.entity.College;
import com.jbl.te.entity.Department;
import com.jbl.te.entity.vo.DepartmentQuery;
import com.jbl.te.entity.vo.DepartmentVo;
import com.jbl.te.service.CollegeService;
import com.jbl.te.service.DepartmentService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jbl
 * @since 2020-12-11
 */
@RestController
@RequestMapping("/te/department")
@CrossOrigin
public class DepartmentController {

    @Autowired
    DepartmentService departmentService;
    @Autowired
    CollegeService collegeService;

    @GetMapping("/getAllDepartment")
    public R getAllDepartment() {
        return R.ok().data("list",departmentService.list());
    }

    @PostMapping("{page}/{limit}")
    @ApiOperation("分页系列表")
    public R pageList(@PathVariable("page") Long page, @PathVariable("limit") Long limit, @RequestBody(required = false) DepartmentQuery departmentQuery) {
        Page<Department> pageParam = new Page<>(page, limit);
        departmentService.pageQuery(pageParam,departmentQuery);
        List<Department> records = pageParam.getRecords();
        List<DepartmentVo> list = new ArrayList<>();
        for (Department department : records) {
            DepartmentVo vo = new DepartmentVo();
            BeanUtils.copyProperties(department,vo);
            College college = collegeService.getById(department.getCollegeId());
            vo.setCollegeName(college.getName());
            list.add(vo);
        }
        long total = pageParam.getTotal();
        return R.ok().data("total", total).data("list", list);
    }
    @PostMapping("saveDepartment")
    @ApiOperation("添加系(TODO)")
    public R saveDepartment(@RequestBody Department department) {

        boolean b = departmentService.save(department);
        if(b) {
            return R.ok();
        } else {
            return R.error().message("添加失败");
        }
    }
    @GetMapping("getDepartmentById")
    @ApiOperation("根据id查找系")
    public R getCollegeById(String id) {
        Department department = departmentService.getById(id);
        if(department == null) {
            return R.error().message("系不存在");
        }
        DepartmentVo vo = new DepartmentVo();
        BeanUtils.copyProperties(department,vo);
        College college = collegeService.getById(department.getCollegeId());
        vo.setCollegeName(college.getName());
        return R.ok().data("department",vo);
    }
    @PutMapping("updateDepartment")
    @ApiOperation("修改系")
    public R updateDepartment(@RequestBody Department department) {
        boolean b = departmentService.saveOrUpdate(department);
        if(b) {
            return R.ok();
        } else {
            return R.error().message("修改失败");
        }
    }
    @DeleteMapping("delDepartmentById")
    @ApiOperation("删除系")
    public R delDepartmentById(String id) {
            //删除功能 TODO
            return R.error().message("暂不支持删除功能");
//        boolean b = departmentService.removeById(id);
//        if(b) {
////            return R.ok();
//        } else {
//            return R.error().message("删除失败");
//        }
    }
    @DeleteMapping("delDepartmentByList")
    @ApiOperation("批量删除系")
    public R delDepartmentsByList(@RequestBody List<String> idList) {
            //删除功能 TODO
            return R.error().message("暂不支持删除功能");
//        boolean b = departmentService.removeByIds(idList);
//        if(b) {
////            return R.ok();
//        }
//        return R.error().message("批量删除失败");
    }

}

